package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.NonMemGroupReserve;
import com.eling.elcms.travel.model.NonMemberGroup.Status;
import com.eling.elcms.travel.model.view.NonMemberGroupView;
import com.eling.elcms.travel.service.INonMemGroupReserverManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
@Controller
public class NonMemGroupReserveController extends BaseFormController{
	@Autowired
	private INonMemGroupReserverManager nonMemGroupReserveManager;
	
	@RequestMapping("/api/nonmemgroupreserve/save")
    @ResponseBody
	public NonMemGroupReserve save(@ModelAttribute@ModelFilling NonMemGroupReserve cond){
		return nonMemGroupReserveManager.save(cond);
	}
	
	@RequestMapping("/api/nonmemgroupreserve/{pkNonMemGroupReserve}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkNonMemGroupReserve){
		nonMemGroupReserveManager.remove(pkNonMemGroupReserve);
		return new Msg("删除成功");
	}
	
	@RequestMapping("/api/nonmemgroupreserve/query")
    @ResponseBody
	public List<NonMemGroupReserve> query(@ModelAttribute NonMemGroupReserve cond,
			@RequestParam(value="nonstatus",required = false) String nonstatus){
		//由于statusIn不可用 ， 所以写这么复杂。
		if(nonstatus == null && nonstatus!=""){
			return nonMemGroupReserveManager.query(cond);
		}else if("All".equals(nonstatus)){
			List<NonMemGroupReserve> list = nonMemGroupReserveManager.query(cond);
			List<NonMemGroupReserve> returnList = new ArrayList<>();
			for (NonMemGroupReserve non : list) {
				if(non.getNonMemberGroup().getStatus() != Status.Cancel || non.getNonMemberGroup().getStatus() != Status.CheckOut){
					returnList.add(non);
				}
			}
			return returnList;
		}else {
			Status status = Status.valueOf(nonstatus);
			PropertyUtils.setProperty(cond, "nonMemberGroup.status",status);
			return nonMemGroupReserveManager.query(cond);
		}
	}
	
	@RequestMapping("/api/nonmemgroupreserve/querycharge")
    @ResponseBody
	public List<NonMemGroupReserve> queryCharge(@ModelAttribute NonMemGroupReserve cond){
		List<NonMemGroupReserve> list = nonMemGroupReserveManager.query(cond);
		List<NonMemGroupReserve> returnList = new ArrayList<>();
		
		for (NonMemGroupReserve nonMemGroupReserve : list) {
			if(nonMemGroupReserve.getNonMemberGroup().getDeposit()!=null&&!"".equals(nonMemGroupReserve.getNonMemberGroup().getDeposit().toString())){
				returnList.add(nonMemGroupReserve);
			}
		}
		return returnList;
	}
	
	@RequestMapping("/api/nonmemgroupreserve/search*")
	@ResponseBody
	public List<NonMemGroupReserve> search(@ModelAttribute SearchCondition sc,@ModelAttribute NonMemGroupReserve cond){
		sc.setQueryCondition(cond);
		return nonMemGroupReserveManager.search(sc);
	}
	
	@RequestMapping("/api/nonmemgroupreserve/reserve")
    @ResponseBody
	public NonMemGroupReserve reserve(@ModelAttribute NonMemberGroupView cond){
		return nonMemGroupReserveManager.reserve(cond);
	}
}
